import React, {Component} from 'react';
import {ScrollView, Text} from 'react-native';
import {InputItem, List, WingBlank} from '@ant-design/react-native';
import {Navigation} from '../../../components/UseNavigation/index';
import styles from './styles';

@Navigation
export class Transform extends Component {
  constructor(props) {
    super(props);
    this.state = {
      price: '',
      priceK: '',
      priceW: '',
      priceY: '',
      priceString: '',
      weight: '',
      weightK: '',
    };
  }
  /**
   * value
   * unit
   */
  onChangeP = (value, unit) => {
    if (!value) {
      this.setState({
        price: '',
        priceK: '',
        priceW: '',
        priceY: '',
        priceString: '',
      });
      return;
    }
    const v = value * unit;
    const priceString = this.digitUppercase(v);
    this.setState({
      price: v,
      priceK: v / 1000,
      priceW: v / 10000,
      priceY: v / 100000000,
      priceString,
    });
  };

  digitUppercase = (n) => {
    console.log(n);
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [
      ['元', '万', '亿'],
      ['', '拾', '佰', '仟'],
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);
    var s = '';
    for (var i = 0; i < fraction.length; i++) {
      s += (
        digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]
      ).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);
    for (var i = 0; i < unit[0].length && n > 0; i++) {
      var p = '';
      for (var j = 0; j < unit[1].length && n > 0; j++) {
        p = digit[n % 10] + unit[1][j] + p;
        n = Math.floor(n / 10);
      }
      s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }

    var result =
      head +
      s
        .replace(/(零.)*零元/, '元')
        .replace(/(零.)+/g, '零')
        .replace(/^整$/, '零元整');
    return result;
  };

  onChangeW = (value, unit) => {
    const v = value * unit;
    const ifnumber = isNaN(value);
    console.log('valeu', value);

    console.log('ifnumber', ifnumber);

    this.setState({
      weight: v,
      weightK: v / 1000,
    });
  };

  render() {
    const {
      price,
      priceK,
      priceW,
      priceY,
      priceString,
      weight,
      weightK,
    } = this.state;
    return (
      <ScrollView
        style={{flex: 1}}
        automaticallyAdjustContentInsets={false}
        showsHorizontalScrollIndicator={false}
        showsVerticalScrollIndicator={false}>
        <WingBlank>
          <List renderHeader={'基本数额转换'}>
            <InputItem
              clear
              type="number"
              value={price + ''}
              onChange={(value) => this.onChangeP(value, 1)}
              extra="元"
              placeholder="元">
              <Text>元</Text>
            </InputItem>
            <InputItem
              clear
              type="number"
              value={priceK + ''}
              onChange={(value) => this.onChangeP(value, 1000)}
              extra="千"
              placeholder="千">
              <Text>千元</Text>
            </InputItem>
            <InputItem
              clear
              type="number"
              value={priceW + ''}
              onChange={(value) => this.onChangeP(value, 10000)}
              extra="万"
              placeholder="万">
              <Text>万元</Text>
            </InputItem>
            <InputItem
              clear
              type="number"
              value={priceY + ''}
              onChange={(value) => this.onChangeP(value, 100000000)}
              extra="亿"
              placeholder="亿">
              <Text>亿元</Text>
            </InputItem>
            <Text style={styles.value}>
              大写结果:<Text style={styles.result}>{priceString}</Text>
            </Text>
          </List>

          <List renderHeader={'基本质量转换'}>
            <InputItem
              clear
              type="number"
              value={weight + ''}
              onChange={(value) => this.onChangeW(value, 1)}
              extra="千克"
              placeholder="千克">
              <Text>千克</Text>
            </InputItem>
            <InputItem
              clear
              type="number"
              value={weightK + ''}
              onChange={(value) => this.onChangeW(value, 1000)}
              extra="吨"
              placeholder="吨">
              <Text>吨</Text>
            </InputItem>
          </List>
        </WingBlank>
      </ScrollView>
    );
  }
}
